package com.ujs.code.removeElement;

import java.util.Arrays;

/**
 * @author zhangshihao
 * @create 2023-09-15 15:38
 */
public class MoveZero {
    public static void main(String[] args) {
        MoveZero moveZero = new MoveZero();
        int[] nums = {0};
        moveZero.moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }

    /**
     * 双指针法：去除掉数组中为0的元素，将最后的元素设置为0
     * @param nums
     */
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        int fast = 0, slow = 0;
        while (fast < n) {
            if (nums[fast] != 0) {
                nums[slow] = nums[fast];
                slow++;
            }
            fast++;
        }
        for (int i = slow; i < nums.length; i++) {
            nums[i] = 0;
        }
    }
}
